package oa.core;

import org.springframework.web.context.ContextLoader;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.JedisPoolConfig;

import java.io.*;
import java.util.Enumeration;
import java.util.Properties;

/**
 * Created by wuwenhao on 14/10/27.
 */
public class MyRedisPool {

    public static volatile JedisPool jedisPool;

    private static final String propertiesPath = "Redis_Properties.properties";
    private static final String WEB_INF = "classes/";
    private static String host;
    private static String password;
    private static int port;
    private static int timeOut;
    private static int database;

    public static JedisPool InitRedisPool(){

    	if(jedisPool == null) {
    		synchronized (MyRedisPool.class) {
    			if(jedisPool == null){
    				jedisPool = createRedisPool();
    			}
    		}
    	}

        return jedisPool;
    }

    public static JedisPool createRedisPool() {
        String path = ContextLoader.class.getResource("/").getPath();
        path = path.replace(WEB_INF, propertiesPath);
        InputStream is = null;
        Properties properties = new Properties();
        try{
            is = new FileInputStream(new File(path));
            properties.load(is);
        } catch (IOException e) {
            System.out.println("the properties file not found ######");
            e.printStackTrace();
        }

        host = properties.getProperty("HOST","127.0.0.1").trim();
        password = properties.getProperty("PASSWORD","wr61212");
        port = Integer.parseInt(properties.getProperty("PORT","6379").trim());
        timeOut = Integer.parseInt(properties.getProperty("TIME_OUT","2000").trim());
        database = Integer.parseInt(properties.getProperty("DATABASE","0").trim());

        //GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        JedisPoolConfig config = new JedisPoolConfig();


        /**
         * there we can set some redis config parameters, make the redis work well,
         * according to the environment, example just set the maxIdle.
         */
        config.setMaxIdle(10);
        config.setMinIdle(5);
        config.setTestWhileIdle(false);
        config.setTestOnBorrow(true);
        config.setTestOnReturn(false);
        config.setNumTestsPerEvictionRun(10);
        config.setMinEvictableIdleTimeMillis(1000);
        config.setSoftMinEvictableIdleTimeMillis(10);
        config.setTimeBetweenEvictionRunsMillis(10);

        jedisPool = new JedisPool(config, host, port, timeOut, password, database);
        return jedisPool;
    }
    
    public static Jedis getInstance(){
        InitRedisPool();
        return jedisPool.getResource();
    }


    //the broken flag is used for the conn is expired or some other reasons which result in
    //not get data from server correctly, we should release the connection
    public static void releaseSource(Jedis jedis, boolean isBroken){
        if(jedis == null){
            return;
        }
        if(isBroken){
            jedisPool.returnBrokenResource(jedis);
        }
        jedisPool.returnResource(jedis);
    }


    



    //test the properties file
    public static void main(String args[]) {

        System.out.println(Constant.getMD5Instance().toMd5("oracle"));
        MyRedis myRedis = new MyRedis();



        /*MyRedisPool redis = new MyRedisPool();
        Properties properties = new Properties();

        InputStream is = redis.getClass().getClassLoader().getResourceAsStream("oa/core/Redis_Properties.properties");
        try {
            properties.load(is);
            Enumeration<Object> keys = properties.keys();
            while (keys.hasMoreElements()) {
                String key = (String) keys.nextElement();
                System.out.println(key + properties.getProperty(key));
            }
        } catch (Exception e) {
            System.out.println("file not found");
        }
    }*/
    }
}
